<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/csy/common/wechatLibs.jsp"%>
<!doctype html>
<html>
<head>
<title>订单信息</title>
<meta charset="utf-8">
<meta name="viewport"
	content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
<meta name="format-detection"
	content="telephone=no,email=no,date=no,address=no">

<style type="text/css">
html, body {
	background: #f7f7f7;
}
</style>
</head>
<body>
	<section class="aui-refresh-content">
		<div class="order-list">
			<input type="hidden" id="page" value="1" />
			<div class="aui-tab" id="tab">
				<!-- <div class="aui-tab-item">
					<span>预约记录</span>
				</div> -->
				<div class="aui-tab-item aui-active">
					<span>礼包记录</span>
				</div>
			</div>
			<div id="order_list"></div>
		</div>
	</section>
	<!-- 底部菜单 -->
    <div class="h88 mt20"></div>
    <div class="bottom-nav">
        <ul>
         
            <li class="find-project">
            <a href="/bsc/wechat/findProject" >
                <i class="ico"></i>
                <span>找项目</span>
             </a>
                
            </li>
          
           
            <li class="find-gift">
             <a href="/bsc/wechat/bagList" >
                <i class="ico"></i>
                <span>找礼包</span>
               </a>
            </li>
           
            
            <li class="mine">
            <a href="/bsc/wechat/Gohome" >
                <i class="ico"></i>
                <span>我的</span>
                </a>
            </li>
            
        </ul>
    </div>
</body>
<script type="text/javascript" src="/bsc/static/wechat/js/aui-tab.js"></script>
<script type="text/javascript" src="/bsc/static/wechat/js/aui-dialog.js"></script>
<script src="/bsc/static/wechat/js/aui-pull-refresh.js"></script>
<script type="text/javascript">
var toast = new auiToast();
var index='${index}';
/* var tab = new auiTab({
	element : document.getElementById("tab"),
	index :index,
	repeatClick : false
}, function(ret) {
	 index=ret.index;
	if(index==1){
		doSearch('0');
	}else{
		doBagSearch('0')
	}
}); */

//页面初始化 
$(function() {
	if(index=='2'){
		doBagSearch('0');
	}else{
		doSearch('0');
	}
	

})
function doSearch(clearStatus) {
	toast.loading({
		title : "订单数据加载中"

	})
	if (clearStatus == '0') {
		$("#page").val('1');
	}
	var page = $("#page").val();
	$.ajax({
		type : 'post',
		url : '/bsc/wechat/GoOrder',
		success : function(data) {
			if (data) {
				createList(data)
			} else {
				toast.fail({
					title : "查询失败",
					duration : 2000
				});
			}
			toast.hide();
		},
		error : function() {
			toast.fail({
				title : "网络异常",
				duration : 2000
			});
			toast.hide();
		}
	})
}
//创建列表数据
function createList(data) {
	var recordList = $("#order_list");
	var html = '';
		html = recordList.html();
	for (var i = 0; i < data.length; i++) {
		var row = data[i];
		var order_status = row.order_status;//状态
		var status_name
		var butStr = '';
		var butStr1 = '';
		var beauty_num=row.extend_beauty_num//美颜币
		if(beauty_num==null){
			beauty_num=0;
		}else if(beauty_num==''){
			beauty_num=0;
		}
		var aa=row.subscribe_time;
		//日期转换
		var subscribe_time=new Date(aa).toLocaleDateString();
		if (order_status == '1') {
			status_name = '已预约';
			butStr = '<div class="fr"><a class="btn cance" href="#" onclick="cancelOrder('
					+ row.order_id + ')">取消订单</a></div>'
			butStr1 = '<div class="fr"><a class="btn" href="#" onclick="goPayOrder('
				+ row.order_id + ','+beauty_num+','+row.shop_id+')">去支付</a></div>'
		} else if (order_status == '2') {
			status_name = '服务中';
		} else if (order_status == '3') {
			status_name = '待支付';
			butStr1 = '<div class="fr"><a class="btn" href="#" onclick="goPayOrder('
				+ row.order_id + ','+beauty_num+','+row.shop_id+')">去支付</a></div>'
		} else if (order_status == '4') {
			status_name = '已支付';
		} else if (order_status == '5') {
			status_name = '已完成';
		} else if (order_status == '6') {
			status_name = '已过期';
		} else {
			status_name = '已撤销';
		}
		html += '<div class="order-detail mt20" >';
		html += '<div class="top">';
		html += '<div class="state">';
		html += ' <span class="fl">' + status_name + '</span>';
		html += ' <span class="fr" onclick="showShopDetail('+row.shop_id+')">' + row.shop.shop_name + '（' + row.shop.short_name
				+ '）</span>'; 
		html += '</div>';
		html += ' <div class="box" onclick="showOrderDetail('
				+ row.order_id + ','+order_status+')">';
		html += ' <img class="fl" src="/bsc'+row.bis_nurse_project.cover_photo+'">';
		html += '<div class="intro fl">';
		html += '<h3>' + row.bis_nurse_project.project_name + '</h3>';
		html += '<span class="nurse-price">￥' +beauty_num+ '个颜值</span>';
		html += '<span class="earnest">定金: ￥' + row.order_money
				+ '</span>';
		html += '</div>';
		html += '<div class="fr">';
		html += '<span>x' + row.buy_num + '</span>';
		html += '</div>';
		html += '</div>';
		html += '<div class="operate">';
		html += '<div class="fl">';
		html += '<span>到店时间</span>';
		html += '<span>' +subscribe_time + '</span>';
		html += '</div>'
		html += butStr1;
		html += butStr;
		html += '</div>'
		html += '</div>'
		html += '</div>'
	}
	recordList.html(html);
}
var pullRefresh = new auiPullToRefresh({
	container : document.querySelector('.aui-refresh-content'),
	triggerDistance : 100
}, function(ret) {
	if (ret.status == "success") {
		pullRefresh.cancelLoading(); //刷新成功后调用此方法隐藏
		var pageStr = $("#page").val();
		var page = parseInt(pageStr) + 1;
		$("#page").val(page);
		if(index==1){
			doSearch('1');
		}else{
			doBagSearch('1');
		}
		

	}
})
//撤销订单
function cancelOrder(order_id) {
	var toast = new auiToast();
	var dialog = new auiDialog({})
	 dialog.alert({
         title:"确认提示",
         msg:'是否取消？',
         buttons:['是 ','否']
     },function(ret){
        if(ret.buttonIndex==1){
        	
    		toast.loading({
    			title : "订单正在取消中"

    		})
    		$.ajax({
    			type : 'post',
    			url : '/bsc/wechat/cancellation',
    			data : {
    				order_id : order_id
    			},
    			dataType : 'json',
    			success : function(data) {

    				if (data) {
    					if (data == "1") {
    						toast.fail({
    							title : "订单已撤销",

    							duration : 2000
    						});
    						//doSearch('0');
    						window.location.href = "/bsc/wechat/Gohome";
    					} else {
    						toast.fail({
    							title : data.appmsg,
    							duration : 2000
    						});
    					}
    				} else {
    					toast.fail({
    						title : "订单取消失败，系统处理异常",
    						duration : 2000
    					});
    				}
    			},
    			error : function() {
    				toast.fail({
    					title : "网络异常",
    					duration : 2000
    				});
    			}
    		})
    		toast.hide();
        }
        
     })

}

function showShopDetail(shop_id){
 window.location.href = '/bsc/wechat/GoshowShopDetail?shop_id='+shop_id;
 }
//跳转到支付页面
function goPayOrder(order_id,beauty_num,shop_id) {
	
	var toast = new auiToast();
	var dialog = new auiDialog({})
	 dialog.alert({
         title:"确认提示",
         msg:'真的要付款吗？',
         buttons:['付款 ','取消']
     },function(ret){
        if(ret.buttonIndex==1){
        	
    		toast.loading({
    			title : "付款正在取消中"

    		})
    		$.ajax({
    			type : 'post',
    			url : '/bsc/wechat/goPayOrder',
    			data : {
    				order_id : order_id,
    				beauty_num:beauty_num,
    				shop_id:shop_id
    			},
    			dataType : 'json',
    			success : function(data) {
    				if (data) {
    					if (data == "1") {
    						toast.fail({
    							title : "付款成功",
    							duration : 2000
    						});
    						//doSearch('0');
    						window.location.href = "/bsc/wechat/Gohome";
    					} else {
    						toast.fail({
    							title : data.appmsg,
    							duration : 2000
    						});
    					}
    				} else {
    					toast.fail({
    						title : "订单取消失败，系统处理异常",
    						duration : 2000
    					});
    				}
    			},
    			error : function() {
    				toast.fail({
    					title : "网络异常",
    					duration : 2000
    				});
    			}
    		})
    		toast.hide();
        }
        
     })
}

//查看订单详情
function showOrderDetail(order_id,order_status) {
	alert(order_id);
	window.location.href = '/bsc/wechat/GoOrder_details?order_id='
			+ order_id+'&order_status='+order_status;
}
//购买礼包记录
function doBagSearch(clearStatus){
	toast.loading({
		title : "订单数据加载中"

	})
	if (clearStatus == '0') {
		$("#page").val('1');
	}
	var page = $("#page").val();
	$.ajax({
		type : 'post',
		url : '/bsc/wechat/GoOrder',
		data : {
			page : page,
			rows : 5
		},
		dataType : 'json',
		success : function(data) {
			if (data) {
				createBagList(data)
			} else {
				toast.fail({
					title : "查询失败",
					duration : 2000
				});
			}
			toast.hide();
		},
		error : function() {
			toast.fail({
				title : "网络异常",
				duration : 2000
			});
			toast.hide();
		}
	})
}
//创建列表数据
function createBagList(data) {
	var recordList = $("#order_list");
	var html = '';
		html = recordList.html();
	for (var i = 0; i < data.length; i++) {
		var row = data[i];
		var order_status = row.order_status;
		var status_name
		var butStr = '';
		if (order_status == '1') {
			status_name = '已预约';
			butStr = '<div class="fr"><a class="btn cance" href="#" onclick="cancelOrder('
					+ row.order_id + ')">取消订单</a></div>'
		} else if (order_status == '2') {
			status_name = '服务中';
		} else if (order_status == '3') {
			status_name = '待支付';
			butStr = '<div class="fr"><a class="btn" href="#" onclick="goPayOrder('
				+ row.order_id + ')">去支付</a></div>'
		} else if (order_status == '4') {
			status_name = '已支付';
		} else if (order_status == '5') {
			status_name = '已完成';
		} else if (order_status == '6') {
			status_name = '已过期';
		} else {
			status_name = '已撤销';
		}
		 
		html += '<div class="order-detail mt20" >';
		html += '<div class="top">';
		html += '<div class="state">';
		html += ' <span class="fl">' + status_name + '</span>';
		html += ' <span class="fr"></span>';
		html += '</div>';
		html += ' <div class="box" onclick="showBagOrderDetail('
				+ row.order_id + ',\''+row.overdue_date+'\')">';
		/* html += ' <img class="fl" src="http://localhost:8080/upload/'+row.cover_photo+'">'; */
		html += ' <img class="fl" src="/bsc/static/images/1.jpeg">';
		html += '<div class="intro fl">';
		html += '<h3>' + row.bag_name + '</h3>';
		html += '<span class="subheading">' + row.project_content + '</span>';
		html += ' <p>￥'+row.pay_money+'</p>';
		html += '</div>';
		html += '<div class="fr">';
		html += '<span>x' + row.buy_num + '</span>';
		html += '</div>';
		html += '</div>';
		html += '<div class="operate">';
		html += '<div class="fl">';
		html += '<span>过期时间</span>';
		html += '<span>' + row.overdue_date + '</span>';
		html += '</div>'
		html += butStr;
		html += '</div>'
		html += '</div>'
		html += '</div>'
	}
	recordList.html(html);
}
function showBagOrderDetail(order_id,overdue_date)	{
	window.location.href = '${ctx}/wechat/order/showBagOrderDetail.jhtml?order_id='
		+ order_id+"&overdue_date="+overdue_date;
}

</script>
</html>